/*******************************************************************************
 * SDP Project AUT 2012
 * 
 *                ***********Driving Assistant************
 * Team:
 * Gisela, Deidre, Brijesh, Robert
 * 
 * This class sets up the LOGO panel which displays the name of the system UI.
 * 
 * @version 1.0
 * 
 * Copyright (c) 2012 by DrivingAssistant Team, Inc., All Rights Reserved.
 * 
 ******************************************************************************/
package com.drivingAssistant.gui;

import java.awt.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

/**
 *
 * Logo panel class
 * 
 */
public class Logo extends JPanel {
    
    //constants
    private static final int TABLE_WIDTH = 600;    // Pixels.
    private static final int TABLE_HEIGHT = 115;
    private static final String LOGO = "./logo/logo2.png";
    
    //variables
    private Image image = null;
    
    /**
     * Constructor to set up objects for the Logo panel
     */
    public Logo() {
        setPreferredSize(new Dimension(TABLE_WIDTH, TABLE_HEIGHT));
        try {
            getFileImage(LOGO);
            repaint();
        } catch (Exception ex) {
            System.err.println("Problem finding image file.." + ex);
        }
    }
    
    /**
     * Get the image
     * 
     * @param filePath which is the directory location of the image
     */
    private void getFileImage(String filePath) {
        
        FileInputStream in = null;
        try {
            in = new FileInputStream(filePath);
            byte[] b = new byte[in.available()];
            in.read(b);
            image = Toolkit.getDefaultToolkit().createImage(b);
            MediaTracker mt = new MediaTracker(this);
            mt.addImage(image, 0);
            mt.waitForAll();
        } catch (InterruptedException ex) {
            Logger.getLogger(Logo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Logo.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if(in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(Logo.class.getName()).log(Level.SEVERE, null, 
                        ex);
            }
        }
    }
    
    /**
     * Paint LOGO
     * 
     * @param g which is the graphic variable
     */
    @Override
    public void paintComponent(Graphics g) {
        //... Paint background
        if (image != null) {
            g.drawImage(image, 0, 0, this);
        } else {
            g.drawString("Hello...Imagefile is missing", 40, 40);
        }
    }
    
}
